 /*
 * redmicro all Copyright (c) 
 * Created by seeker910 on 2015/6/30.
 * 文件夹视图
 */
Rsd.define('Rsd.widget.FolderViewer', {
    extend: 'Rsd.container.Component',
    requires: [
        'Rsd.form.Text',
        'Rsd.form.TipButton',
        'Rsd.control.Svg',
        'Rsd.control.List',
        'Rsd.control.Grid',
        'Rsd.control.SearchToolBar',
        'Rsd.control.PagingBar'
    ],
    xtype: 'folder-viewer',
    border: false,
    height: 800,
    //width: "80%", 
    layout: 'hbox',
    items: [
        {
            "name": "left",
            xtype: "list",
            label:{
                content:'',
                height:50,
                style:{  
                    width:'95%',
                    textAlign:'left',
                    fontSize: '120%',
                    boxSizing:'border-box',
                    borderBottom:'1px #7c7c8f solid'
                }, 
            },
            width: 150, 
            height:'100%', 
            itemXtype: 'image',
            itemDblClick:'left_item_DblClick',
            itemStyle: {
                margin:'8px 10% 2px 10%',
                width: '80%', 
                height: 85
            },
            usingItemHover:'x-folder-hover',
            itemSelectedCls:'x-folder-selected',
            layout:'none',
            style:{
                backgroundColor: '#4f596103'
            }
        }, 
        {
            "name": "content",
            xtype: "container", 
            height: '100%',
            flex: 1,
            header: {
                xtype: 'search-tool-bar',
                position: 'top',
                height: 50,
                space: 10,
                header: {
                    position: 'left',
                    space: 0,
                    content: '文件列表',
                    width: 100,
                    align: 'left',
                    style: {
                        fontSize: '120%',
                        backgroundColor: 'transparent',
                        letterSpacing: '2px',
                    }
                },
                layout: 'hbox',
                items: [],
                style: {
                    display:'flex',  
                    backgroundColor: '#4f59610f',
                    borderBottom: '1px #7c7c8f solid'
                }
            },  
            layout:'vbox',
            items: [ 
                { 
                    xtype: "list",
                    label:false,
                    flex: 1,
                    width: '100%', 
                    showCheckbox:false,
                    itemCheckedChange:'file_itemCheckedChange',
                    itemXtype: 'image',
                    itemDblClick: 'file_DblClick',
                    itemStyle: {
                        margin: '8px 5px 10px 8px',
                        width: 120,
                        height: 120
                    }, 
                    usingItemHover:'x-folder-hover',
                    layout: { type: 'hbox', align: 'left' }
                },
                {
                    xtype: 'grid',
                    label:false,
                    hidden: true,
                    flex: 1,
                    width: '100%',
                    rowdblclick: 'grid_row_dblclick',
                    rowclick:''
                },
                {
                    xtype:'paging-bar',
                    indexChanged:'page_index_changed'
                }
            ]
        }
    ],
    /**
     * 左侧对象双击
     */
    leftItemDblClickHandler:'',
    /**
     * 文件双击事件
     */
    fileItemDblClickHandler:null,  
    /**
     * 文件选中 状态发生改变
     */
    fileItemCheckedChange:null,
    /**
     * 
     */
    rowDblClickHandler:null,
    /**
     * 
     */
    pageIndexChangedHandler:null, 
    /**
     * 
     */
    constructor: function FolderViewer(config) {
        config = config || {};
        this.apply(config); 
    },
    /**
     * 
     */
    loadData: function loadData(leftItems,listItems,gridRows) {
 
        this.loadLeft(leftItems);
        this.loadContent(listItems,gridRows);
    },
    /**
     * 
     */
    loadLeft:function loadLeft(leftItems,callback)
    { 
        this.items[0].loadData(leftItems,callback); 
    },
    /**
     * 
     * @param {*} contentItems 
     */
    loadContent: function loadContent(listItems,gridRows) {
         
        this.items[1].items[0].loadData(listItems); 
        this.items[1].items[1].loadData(gridRows); 
    },
    
    /**
    * @description 获取分页信息,在查询时，自动加上分页信息
    * */
    getPagingOpt:function getPagingOpt() {
        
      if(this.items[1].items[2] instanceof  Rsd.control.PagingBar)
      {
          return this.items[1].items[2].getPaging();
      }
      return {pageIndex:0,pageSize:1000};
    },

    /**
    * @description 设置分页信息,一般在查询条件发发生改变时，需重新设置分页 {pageIndex: 0}
    * */
    setPagingOpt:function setPagingOpt(opt) {
 
        if(this.items[1].items[2] instanceof  Rsd.control.PagingBar)
        {
            return this.items[1].items[2].setPaging(opt);
        }
    },
    /**
     * 
     * @param {*} pagingOpt 
     */
    page_index_changed:function page_index_changed(pagingOpt)
    {
        if(this.pageIndexChangedHandler)
        {
            this.funApplyByIOC(this.pageIndexChangedHandler, [pagingOpt]);
        }
    }, 
    /**
     * 展示缩略图 视图
     */
    showList:function showList()
    {
        this.items[1].items[1].hide();
        this.items[1].items[0].show();
    },
    /**
     * 展示表格 视图
     */
    showGrid:function showGrid()
    {
         this.items[1].items[0].hide();
        this.items[1].items[1].show();
    },
    /**
     * 
     * @param {*} sender 
     * @param {*} evt 
     */
    left_item_DblClick:function left_item_DblClick(sender,evt,data)
    {
        if(this.leftItemDblClickHandler)
        {
            this.funApplyByIOC(this.leftItemDblClickHandler, [sender, evt,data]);
        } 
    },
     /**
     * @description 双击文件表示  
     * @param {*} item listItem
     * @param {*} evt 
     */
    file_DblClick: function file_DblClick(sender, evt,data) {
        
        if(this.fileItemDblClickHandler)
        {
            this.funApplyByIOC(this.fileItemDblClickHandler, [sender, evt,data]);
        } 
    },
    /**
     * 
     * @param {*} row 
     * @param {*} evt 
     */
    grid_row_dblclick:function grid_row_dblclick(row,evt,sender)
    { 
        if(this.rowDblClickHandler)
        { 
            this.funApplyByIOC(this.rowDblClickHandler, [row, evt,sender]);
        } 
    },
    /**
     * 
     * @param {*} sender 
     * @param {*} evt 
     * @param {*} config 
     */
    file_itemCheckedChange:function file_itemCheckedChange(sender,evt,config)
    {   
        if(this.fileItemCheckedChange)
        {
            this.funApplyByIOC(this.fileItemCheckedChange, [sender,evt,config]);
        }
    }
    
},function(type){

    //leftLabel
    var _leftLabelGetter = function () {
 
        return this.items[0].label;
    };

    var _leftLabelSetter = function (value) {

        if(Rsd.isString(value))
        {
            this.items[0].label.content = value;
        }
        if(Rsd.isObject(value))
        {
            Rsd.apply(this.items[0].label, value);  
        } 
  
    };
    this.defineProperty(type, "leftLabel", _leftLabelGetter, _leftLabelSetter, false);
    
    //contentHeader
    var _contentHeaderGetter = function () {
 
        return this.items[1].header;
    };

    var _contentHeaderSetter = function (value) {
        if(Rsd.isString(value))
        {
            this.items[1].header.header.content = value;
        }
        if(Rsd.isObject(value))
        {
            Rsd.apply(this.items[1].header, value); 
        } 
    };
    this.defineProperty(type, "contentHeader", _contentHeaderGetter, _contentHeaderSetter, false);
  
    //contentColumns
    var _contentColumnsGetter = function () {
 
        return this.items[1].items[1].columns;
    };

    var _contentColumnsSetter = function (value) {
        if(Rsd.isArray(value))
        {
            this.items[1].items[1].columns = value; 
        } 
    };
    this.defineProperty(type, "contentColumns", _contentColumnsGetter, _contentColumnsSetter, false);
    //left
    var _leftGetter = function () {
 
        return this.items[0];
    };
    var _leftSetter = function (value)
    {
        if(Rsd.isObject(value))
        {
            Rsd.apply(this.items[0],value,2);
            // console.log('this is left',this.items[0]);
        }
         
    }
    this.defineProperty(type, "left", _leftGetter, _leftSetter, false);
    
    //content
    var _contentGetter = function (value) {
         return this.items[1];
    }; 
    var _contentSetter = function (value)
    {
        if(Rsd.isObject(value))
        {
            Rsd.apply(this.items[1],value,2);
        } 
    }
    this.defineProperty(type, "content", _contentGetter, _contentSetter, false);
    
    //multiple
    var _multipleGetter = function (value) {
         return this.___multiple;
    }; 
    var _multipleSetter = function (value)
    {
         this.___multiple = value
    }
    this.defineProperty(type, "multiple", _multipleGetter, _multipleSetter, false);
    
    //showCheckbox
    var _showCheckboxGetter = function (value) {
         return this.items[1].items[0].showCheckbox;
    }; 
    var _showCheckboxSetter = function (value)
    {
        this.items[1].items[0].showCheckbox = value; 
    }
    this.defineProperty(type, "showCheckbox", _showCheckboxGetter, _showCheckboxSetter, false);
});
